package com.itheima.dubbo.api;

import com.itheima.dubbo.util.IdSequence;
import com.tanhua.domain.mogo.FollowUser;
import com.tanhua.domain.mogo.Video;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.VideoApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class VideoApiImpl implements VideoApi {


    @Override
    public List<Video> findVideoByVids(List<Long> vidList) {
        Query query = new Query(Criteria.where("vid").in(vidList));
        return mongoTemplate.find(query,Video.class);
    }

    @Override
    public Video findById(String videoId) {
        return mongoTemplate.findById(new ObjectId(videoId),Video.class);
    }

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdSequence idSequence;
    @Override
    public String save(Video video) {
        video.setId(ObjectId.get());
        video.setCreated(System.currentTimeMillis());
        video.setVid(idSequence.getNextId("video"));
        mongoTemplate.save(video);
        //返回视频的id
        return video.getId().toHexString();

    }

    @Override
    public PageResult<Video> findVideoList(int page, int pagesize) {
        Query query = new Query()
                .with(PageRequest.of(page,pagesize))
                .with(Sort.by(Sort.Order.desc("created")));
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        long count=mongoTemplate.count(query,Video.class);
        int pages=(int) Math.ceil(count*1.0/pagesize);

        return new PageResult<>((int) count,pages,page,pagesize,videoList);
    }
  // 小视频--关注用户
    @Override
    public void followUser(long userId, long followUserId) {
        FollowUser followUser = new FollowUser();
        followUser.setId(ObjectId.get());
        followUser.setUserId(userId);
        followUser.setFollowUserId(followUserId);
        followUser.setCreated(System.currentTimeMillis());
        mongoTemplate.save(followUser);
    }


    //小视频--取消关注
    @Override
    public void disFollowUser(long userId, long followUserId) {
        Criteria criteria=Criteria.where("userId").is(userId)
                .and("followUserId").is(followUserId);
        mongoTemplate.remove(new Query(criteria),FollowUser.class);
    }
    /**
     * 分页查询某用户的视频列表
     * @param page 页码
     * @param pagesize 查询几条
     * @param uid 用户id
     * @return 分页查询结果
     */
    @Override
    public PageResult<Video> fingVideoList(int page, int pagesize, Long uid) {
        Query query = new Query(Criteria.where("userId").is(uid))
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page-1,pagesize));
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        long count=mongoTemplate.count(query,Video.class);
        int pages=(int) Math.ceil(count*1.0/pagesize);

        return new PageResult<>((int)count,pages,page,pagesize,videoList);
    }
}
